<?php

namespace service;

use RongCloud\RongCloud;

/**
 * 融云 Service
 * Class RCloudService
 * @package service
 */
class RCloudService
{
    /**
     * 应用对象
     * Author: Albert3306
     * Date: 2021/4/23
     * @return RongCloud
     */
    public static function application()
    {
        $config = [
            'app_key' => '',
            'app_secret' => '',
        ];
        return new RongCloud($config['app_key'], $config['app_secret']);
    }

    /**
     * 获取单个用户
     * Author: Albert3306
     * Date: 2021/4/23
     * @param array $user 需要查询的用户 ['id' => 1]
     * @return array
     */
    public static function getOneUser(array $user)
    {
        return self::application()->getUser()->get($user);
    }

    /**
     * 发送私聊消息
     * Author: Albert3306
     * Date: 2021/4/23
     * @param string $senderId 发送人 Id
     * @param string $targetId 接收人 Id
     * @param string $objectName 消息类型，除了融云的内置消息，还可以自定义消息内容
     * @param array $content 消息内容 根据消息类型不同，内容格式也各有查询，详细情况参考官方文档
     * @return array
     */
    public static function publishPrivate(string $senderId, string $targetId, string $objectName, array $content)
    {
        return self::application()->getMessage()->Person()->send([
            'senderId' => $senderId,
            'targetId' => $targetId,
            'objectName' => $objectName,
            'content'    => json_encode($content)
        ]);
    }
}